package com.sams.controller;

import com.alibaba.fastjson.JSON;
import com.sams.pojo.Convenient;
import com.sams.pojo.Notice;
import com.sams.pojo.PageBean;
import com.sams.pojo.Query1;
import com.sams.service.Class_Service;
import com.sams.service.ConvenientService;
import com.sams.service.NoticeService;
import com.sams.util.CommonUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.Objects;

@WebServlet("/queryNotice")
public class SelectNotice extends HttpServlet{
    private NoticeService noticeService = new NoticeService();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        BufferedReader reader = request.getReader();
        String params = reader.readLine();
        Query1 query1 = JSON.parseObject(params, Query1.class);
        String _currentPage = request.getParameter("currentPage");
        String _pageSize = request.getParameter("pageSize");
        /*接收到的是String类型的页码信息，我们要转成int才能给方法传参*/
        int currentPage= Integer.parseInt(_currentPage);
        int pageSize = Integer.parseInt(_pageSize);
        if (Objects.isNull(query1)) {
            query1 = new Query1();
        }
        PageBean<Notice> pageBean = noticeService.selectbyPage(currentPage, pageSize, query1);
        System.out.println(pageBean.toString());
        /*把pagebean集合转换为json对象*/
        String jsonString = JSON.toJSONString(pageBean);
        /*用resp响应给前端，并处理中文乱码问题*/
        System.out.println(jsonString);
        response.setContentType("text/json;charset=utf-8");
        response.getWriter().write(jsonString);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
}
